import { handleActions } from 'redux-actions'
import { STUDYDATA, CHANGEHANDOUTBTNTYPE, CHANGSTUDYINGID, SOURCEINFO, CHANGESOURCEINFO, NODEINDEX, CHANGETOPICINFO, CHANGESUBMITDATA} from '../types/study'

const defaultState = {
	data: {},
  handoutBtnType: 2,
  studyingId: '',
  nodeIndex: '01',
  sourceInfo: {},
  topicInfo: {
		index: 0,
		trueNum: 0,
		falseNum: 0,
		isPass: 0,
		btnStatus: false,
		showAnalysis: false,
		eachFlag: 1
	},
	submitData: []
}

export default handleActions({
	[STUDYDATA]( state , action ){
    state.data = action.payload
    state.data.info.totalScore = Number(state.data.info.totalScore)
    state.data.info.userScore = Number(state.data.info.userScore)
		return {
			...state
		}
	},
  [CHANGEHANDOUTBTNTYPE]( state, action){
    state.handoutBtnType = action.payload
		return {
			...state
		}
  },
  [CHANGSTUDYINGID]( state, action){
    state.studyingId = action.payload
		return {
			...state
		}
  },
  [NODEINDEX]( state, action){
    state.nodeIndex = action.payload.nodeIndex
    state.knowledgeNodeName = action.payload.knowledgeNodeName
		return {
			...state
		}
  },
  [SOURCEINFO]( state, action){
    state.sourceInfo = action.payload
		return {
			...state
		}
  },
  [CHANGESOURCEINFO]( state, action){
    let r = action.payload
    if (r.t === 1) { // 修改active
      state.sourceInfo.obj.exerList[r.v.parentIndex].optionList[r.v.index].active = r.v.active
    }
		return {
			...state
		}
  },
  [CHANGETOPICINFO]( state, action){
    state.topicInfo = action.payload
    return {
      ...state
    }
  },
  [CHANGESUBMITDATA]( state, action){
    state.submitData = action.payload
    return {
      ...state
    }
  }
}, defaultState)
